extends Camera2D

var target_position=Vector2.ZERO

#当相机加载完成时，会自动成为当前相机
func _ready() -> void:
	make_current()


func _process(delta: float) -> void:
	##先将player场景加入group，通过get_tree().get_nodes_in_group("player")方法
	##获取到player场景的所有对象（一个数组），
	##然后调用Sprite2D内置的global_position让相机获取到角色的定位
	#var player_node=get_tree().get_nodes_in_group("player")
	#if player_node.size()>0:
		#var player=player_node[0] as Node2D
		#global_position=player.global_position
	acquire_target()
	#此处的数学公式是为了增加相机平滑程度
	global_position=global_position.lerp(target_position,1.0-exp(-delta * 20))

func acquire_target():
	var player_node=get_tree().get_nodes_in_group("player")
	if player_node.size()>0:
		var player=player_node[0] as Node2D
		target_position=player.global_position
